package com.ruoyi.jl_base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.jl_base.domain.bo.JlBaseBaContractSheetBo;
import com.ruoyi.jl_base.domain.vo.JlBaseBaContractSheetVo;
import com.ruoyi.jl_base.domain.JlBaseBaContractSheet;
import com.ruoyi.jl_base.mapper.JlBaseBaContractSheetMapper;
import com.ruoyi.jl_base.service.IJlBaseBaContractSheetService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 工程量清单Service业务层处理
 *
 * @author zyb
 * @date 2024-12-23
 */
@RequiredArgsConstructor
@Service
public class JlBaseBaContractSheetServiceImpl implements IJlBaseBaContractSheetService {

    private final JlBaseBaContractSheetMapper baseMapper;

    /**
     * 查询工程量清单
     */
    @Override
    public JlBaseBaContractSheetVo queryById(Long sheetId){
        return baseMapper.selectVoById(sheetId);
    }

    /**
     * 查询工程量清单列表
     */
    @Override
    public TableDataInfo<JlBaseBaContractSheetVo> queryPageList(JlBaseBaContractSheetBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<JlBaseBaContractSheet> lqw = buildQueryWrapper(bo);
        Page<JlBaseBaContractSheetVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询工程量清单列表
     */
    @Override
    public List<JlBaseBaContractSheetVo> queryList(JlBaseBaContractSheetBo bo) {
        LambdaQueryWrapper<JlBaseBaContractSheet> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<JlBaseBaContractSheet> buildQueryWrapper(JlBaseBaContractSheetBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<JlBaseBaContractSheet> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getContractId() != null, JlBaseBaContractSheet::getContractId, bo.getContractId());
        lqw.eq(bo.getParentSheetId() != null, JlBaseBaContractSheet::getParentSheetId, bo.getParentSheetId());
        lqw.eq(StringUtils.isNotBlank(bo.getZmh()), JlBaseBaContractSheet::getZmh, bo.getZmh());
        lqw.eq(StringUtils.isNotBlank(bo.getZmmc()), JlBaseBaContractSheet::getZmmc, bo.getZmmc());
        lqw.eq(StringUtils.isNotBlank(bo.getDanwei()), JlBaseBaContractSheet::getDanwei, bo.getDanwei());
        return lqw;
    }

    /**
     * 新增工程量清单
     */
    @Override
    public Boolean insertByBo(JlBaseBaContractSheetBo bo) {
        JlBaseBaContractSheet add = BeanUtil.toBean(bo, JlBaseBaContractSheet.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setSheetId(add.getSheetId());
        }
        return flag;
    }

    /**
     * 修改工程量清单
     */
    @Override
    public Boolean updateByBo(JlBaseBaContractSheetBo bo) {
        JlBaseBaContractSheet update = BeanUtil.toBean(bo, JlBaseBaContractSheet.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(JlBaseBaContractSheet entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除工程量清单
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
